#include <stdio.h>

/* 将一个长整型值从小端转到大段或者相反 */
#define BYTESWAP(val) \
    __asm__ __volatile__ ( \
        "eor     r3, %1, %1, ror #16\n\t" \
        "bic     r3, r3, #0x00FF0000\n\t" \
        "mov     %0, %1, ror #8\n\t" \
        "eor     %0, %0, r3, lsr #8" \
    : "=r" (val) \
    : "0"(val) \
    : "r3", "cc" \
        );

int test1(void)
{
    int tmp = 5;

    asm (
        "mov r4, %0\n"
        :
        :"r"(tmp)
        :"r4"
        );

    printf("tmp = %d\n", tmp);
    return 0;
}

int test1_1(void)
{
    int tmp;

    asm (
        "mov %0, #1\n"
        :"=r"(tmp)
        :
        );

    printf("tmp = %d\n", tmp);
}

int test2()
{
    int nn = 10;

    asm volatile ( 
        "0:                         \n"
        "subs   %0, %0, #1        \n"
        "bne    0b                  \n"
        : "=r"(nn)
        : "0"(nn)
        : 
        );
 
    printf("nn=%d\n", nn);
    return 0;
}

static __inline__ void __set_bit(int nr, volatile void * addr)
{ 
    __asm__( 
        "btsl %1,%0" 
        :"=m" (addr) 
        :"Ir" (nr)); 
}

void test3()
{
    int a = 2;
    int b = 0;

    asm (
        "mov %[result], %[value], ror #1 \n\t"
        : [result] "=r" (b)
        : [value] "r" (a)
        );

    printf("a = %d b = %d\n", a, b);
}

void test4()
{
    int a = 2;
    int b = 0;

    asm(
        "mov %[result], %[value], lsl #1 \n\t"
        : [result] "=r"(b)
        : [value] "r" (a)
        );

    printf("a = %d b = %d\n", a, b);
}

void test5()
{
    int a = 2;
    int b = 0;

    asm(
        "mov %0, %1, lsl #1 \n\t"
        :"=r"(b)
        :"r" (a)
        );

    printf("a = %d b = %d\n", a, b);
}

void main()
{
    test1();
    test1_1();
    test2();
    test3();
    test4();
    test5();
}
